package sword_to_1;

import node.ListNode;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_24 {
    public static void main(String[] args) {
        System.out.println(reverseList(new ListNode(1, new ListNode(2,
                new ListNode(3, new ListNode(4, new ListNode(5)))))));
    }
    private static ListNode reverseList(ListNode head) {
        if (head == null){
            return null;
        }
        List<ListNode> list = new ArrayList<>();
        while (head != null){
            list.add(head);
            head = head.next;
        }
        for(int i = list.size() - 1; i > 0 ; i--){
            list.get(i).next = list.get(i - 1);
        }
        list.get(0).next = null;
        return list.get(list.size() - 1);
    }
}
